import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;



/**
 * The TicketGrid creates the container that will hold orders for individual
 * seats on the Server screen. This has been designed as to allow the server to
 * enter the number of customers at a table.
 * 
 * @author Mike Jones
 */
public class TicketGrid extends JPanel {

	private static class MenuItem {
		String itemName;
		double itemPrice;

		public MenuItem(String i, double p) {
			itemName = i;
			itemPrice = p;
		}
		
		public String toString() {
			return itemName + " " + itemPrice;
		}
	}

	public static int custNum = 0;
	private JPanel mainPanel;
	private JPanel panel;
	private JPanel numbersPanel;
	private JTextArea order;
	private JTextField amount;
	private JButton header;
	private JScrollPane scroll;
	static ArrayList<JTextArea> arrayTA;
	private static ArrayList<JButton> arrayJB;
	private static int seatNum;
	private static boolean isHeaderClicked;
	
	private static HashMap<Integer, ArrayList<MenuItem>> seatItems = new HashMap<>();

	private Toolkit tk = Toolkit.getDefaultToolkit();
	private static int tableNumber;

	/**
	 * Holds the values for the maximum height and width for the user screen
	 * size.
	 */
	private int screenWidth = ((int) tk.getScreenSize().getWidth());
	private int screenHeight = ((int) (tk.getScreenSize().getHeight()));

	/**
	 * Builds the ticket displayer
	 */
	public TicketGrid(int customers) {
		this.setLayout(new BorderLayout());
		mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
		// this call needs to be an action listener from the square table
		custNum = 0;
		makePanel(customers);
		tableNumber = GUI.getTableNumber();
	}// End of Constructor

	/**
	 * Makes as many ticket panels as there are customers - scrollbar is set up
	 * so that only 3 tickets will be displayed at one time
	 * 
	 * @param c
	 *            - the number of customers
	 */
	private void makePanel(int c) {
		custNum = c;
		arrayJB = new ArrayList<JButton>();
		arrayTA = new ArrayList<JTextArea>();

		for (int i = 1; i <= c; i++) {
			panel = new JPanel(new BorderLayout());
			panel.setBorder(BorderFactory.createLineBorder(Color.white, 5));
			header = new JButton("Seat " + i);
			header.setFont(new Font("Arial", Font.PLAIN, 40));
			header.setBackground(new Color(91, 155, 213));
			header.setForeground(Color.white);

			header.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String buttonLabel = e.getActionCommand();
					setSeatNumber(Integer.parseInt(buttonLabel
							.substring(buttonLabel.indexOf(" ") + 1)));
					setIsHeaderClicked(getSeatNumber());
				}
			});

			panel.add(header, BorderLayout.NORTH);
			order = new JTextArea();
			order.setEditable(false);
			order.setLineWrap(true);
			order.setWrapStyleWord(true);
			order.setBackground(new Color(210, 222, 239));
			order.setPreferredSize(order.getPreferredSize());
			order.setFont(new Font("Arial", Font.PLAIN, 25));
			panel.setPreferredSize(new Dimension(screenWidth / 4, screenHeight));
			panel.add(order);
			mainPanel.add(panel);

			arrayJB.add(header);// Used to keep location of a certain JButton
								// header (always minus one)
			arrayTA.add(order);// Used to keep location of a certain JTextArea
								// (always minus one)
		}
		// if more than 3 customers at a table display the scroll bar
		if (c > 3) {
			scroll = new JScrollPane(mainPanel);
			scroll.setHorizontalScrollBarPolicy(scroll.HORIZONTAL_SCROLLBAR_ALWAYS);
			scroll.getVerticalScrollBar().setPreferredSize(
					new Dimension(screenWidth / 25, 0));
			scroll.getHorizontalScrollBar().setPreferredSize(
					new Dimension(0, screenHeight / 20));
			this.add(scroll, BorderLayout.CENTER);
		} else {
			this.add(mainPanel);
		}

	}// End of makePanel method

	/**
	 * Sets the boolean isHeaderClicked to ensure only one seat number button is
	 * highlighted at a time to add to orders Clicking on a already highlighted
	 * button will unhighlight the button
	 * 
	 * @param n
	 *            - the seat number in the array of seat number buttons
	 */
	public static void setIsHeaderClicked(int n) {

		if (n == -1) {
			isHeaderClicked = false;
			for (JButton jb : arrayJB) {
				jb.setBackground(new Color(91, 155, 213));
				jb.setForeground(Color.white);
			}
		} else if (isHeaderClicked == false
				&& arrayJB.get(n).getForeground() == Color.WHITE) {
			arrayJB.get(n).setBackground(Color.YELLOW);
			arrayJB.get(n).setForeground(Color.BLACK);
			isHeaderClicked = true;
		} else if (isHeaderClicked == true
				&& arrayJB.get(n).getForeground() == Color.BLACK) {
			arrayJB.get(n).setBackground(new Color(91, 155, 213));
			arrayJB.get(n).setForeground(Color.white);
			isHeaderClicked = false;
		} else {
			// do nothing
		}

	}// End of setIsHeaderClicked

	/**
	 * Returns the boolean value of getIsHeaderClicked
	 * 
	 * @return isHeaderClicked - true or false as to whether a seat number
	 *         button is clicked
	 */
	public static boolean getIsHeaderClicked() {
		return isHeaderClicked;
	}// End getIsHeaderClicked method

	/**
	 * Sets the integer that contains the seat number button that was selected
	 * 
	 * @param n
	 *            - the seat number button in ArrayJB
	 */
	private static void setSeatNumber(int n) {
		seatNum = n - 1;
	}// end setSeatNumber

	/**
	 * Gets the integer that contains the seat number button that was selected
	 * 
	 * @return seatNum - the seat number button in ArrayJB
	 */
	public static int getSeatNumber() {
		return seatNum;
	}// end getSeatNumber

	public static ArrayList<JButton> getArrayJB() {
		return arrayJB;
	}

	/**
	 * Sets the JTextArea that contains the ticket order. "Price" is a
	 * placeholder for the actual price.
	 * 
	 * @param itemName
	 *            - the item to be added to the Order JTextArea
	 */
	public static void setTicketOrder(String itemName) {
		double itemPrice = SqliteHelper.GetPriceByItemName(itemName);
		if (!seatItems.containsKey(getSeatNumber())) { 
			seatItems.put(getSeatNumber(), new ArrayList<MenuItem>());
			
		}
		
		seatItems.get(getSeatNumber()).add(new MenuItem(itemName, itemPrice));
		BigDecimal totalPrice = new BigDecimal("0.00");
		for (MenuItem i : seatItems.get(getSeatNumber())) {
			totalPrice = totalPrice.add(new BigDecimal(i.itemPrice));
		}
		
		
		totalPrice = totalPrice.setScale(2, BigDecimal.ROUND_HALF_EVEN);
		arrayTA.get(getSeatNumber()).setText(StringUtils.join(seatItems.get(getSeatNumber()), "\n") + "\n\nTotal: " + totalPrice);
		
		SqliteHelper.InsertOrder(getSeatNumber(), tableNumber, itemName, itemPrice);
		
		
		arrayTA.get(seatNum).setFont(new Font("Arial", Font.PLAIN, 30));
	}// End getSeatNumber
	
	public static void sendTicketToKitchen(int custNumber) {
		seatItems.remove(custNumber);
	}
	
	public static void closeTicket(int custNumber) {
		seatItems.remove(custNumber);
	}

}// End of Class
